@@ -31,6 +31,9 @@ gem 'omniauth-tumblr' |
||
31 | 31 |
gem 'dropbox-api' |
32 | 32 |
gem 'omniauth-dropbox' |
33 | 33 |
|
34 |
+# UserLocationAgent |
|
35 |
+gem 'haversine' |
|
36 |
+ |
|
34 | 37 |
# Optional Services. |
35 | 38 |
gem 'omniauth-37signals' # BasecampAgent |
36 | 39 |
# gem 'omniauth-github' |
@@ -170,6 +170,7 @@ GEM |
||
170 | 170 |
guard (~> 2.1) |
171 | 171 |
rspec (>= 2.14, < 4.0) |
172 | 172 |
hashie (2.0.5) |
173 |
+ haversine (0.3.0) |
|
173 | 174 |
hike (1.2.3) |
174 | 175 |
hipchat (1.2.0) |
175 | 176 |
httparty |
@@ -476,6 +477,7 @@ DEPENDENCIES |
||
476 | 477 |
guard |
477 | 478 |
guard-livereload |
478 | 479 |
guard-rspec |
480 |
+ haversine |
|
479 | 481 |
hipchat (~> 1.2.0) |
480 | 482 |
httparty (~> 0.13) |
481 | 483 |
hypdf (~> 1.0.7) |
@@ -1,4 +1,5 @@ |
||
1 | 1 |
require 'securerandom' |
2 |
+require 'haversine' |
|
2 | 3 |
|
3 | 4 |
module Agents |
4 | 5 |
class UserLocationAgent < Agent |
@@ -38,7 +39,8 @@ module Agents |
||
38 | 39 |
def default_options |
39 | 40 |
{ |
40 | 41 |
'secret' => SecureRandom.hex(7), |
41 |
- 'max_accuracy' => '' |
|
42 |
+ 'max_accuracy' => '', |
|
43 |
+ 'distance' => '', |
|
42 | 44 |
} |
43 | 45 |
end |
44 | 46 |
|
@@ -75,11 +77,25 @@ module Agents |
||
75 | 77 |
|
76 | 78 |
accuracy_field = interpolated[:accuracy_field].presence || 'accuracy' |
77 | 79 |
|
78 |
- if location.present? && (!interpolated[:max_accuracy].present? || !payload[accuracy_field] || payload[accuracy_field].to_i < interpolated[:max_accuracy].to_i) |
|
80 |
+ def accurate_enough?(payload, accuracy_field) |
|
81 |
+ !interpolated[:max_accuracy].present? || !payload[accuracy_field] || payload[accuracy_field].to_i < interpolated[:max_accuracy].to_i |
|
82 |
+ end |
|
83 |
+ |
|
84 |
+ def far_enough?(payload) |
|
85 |
+ if memory['last_location'].present? |
|
86 |
+ travel = Haversine.distance(memory['last_location']['latitude'].to_i, memory['last_location']['longitude'].to_i, payload['latitude'].to_i, payload['longitude'].to_i).to_meters |
|
87 |
+ !interpolated[:distance].present? || travel > interpolated[:distance].to_i |
|
88 |
+ else # for the first run, before "last_location" exists |
|
89 |
+ true |
|
90 |
+ end |
|
91 |
+ end |
|
92 |
+ |
|
93 |
+ if location.present? && accurate_enough?(payload, accuracy_field) && far_enough?(payload) |
|
79 | 94 |
if interpolated[:max_accuracy].present? && !payload[accuracy_field].present? |
80 | 95 |
log "Accuracy field missing; all locations will be kept" |
81 | 96 |
end |
82 | 97 |
create_event payload: payload, location: location |
98 |
+ memory["last_location"] = payload |
|
83 | 99 |
end |
84 | 100 |
end |
85 | 101 |
end |